/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.fee.bean;

//import com.yunhoo.cea.db.impl.DangHuiImpl;
//import com.yunhoo.cea.model.DangHui;
//import com.yunhoo.cea.model.FeeCal;
//import com.yunhoo.cea.model.SalaData;
//import com.yunhoo.pub.bean.Login;
//import com.yunhoo.pub.db.config.IbatisTemplate;
//import com.yunhoo.system.util.pub.FacesUtils;
//import com.yunhoo.system.util.pub.PayCodeConstants;
import com.yunhoo.asset.util.PayCodeConstants;
import com.yunhoo.fee.db.impl.DangHuiImpl;
import com.yunhoo.fee.model.DangHui;
import com.yunhoo.fee.model.FeeCal;
import com.yunhoo.fee.model.SalaData;
import com.yunhoo.sys.db.util.IbatisTemplate;
import com.yunhoo.sys.util.common.CommonParam;
//import com.yunhoo.sys.db.util.IbatisTemplate;
import com.yunhoo.sys.util.common.LoginParam;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFooter;
import org.apache.poi.hssf.usermodel.HSSFHeader;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CreationHelper;

/**
 *
 * @author Administrator
 */
@ManagedBean(name = "feeCal")
@SessionScoped
public class FeeCalBean {

    private String iyear = "";
    private String imonth = "";
    private List<SelectItem> iyearLi = null;
    private String cpsn_number = "";
    private DangHui dang = new DangHui();
    private FeeCal fee = new FeeCal();
    private SalaData data = new SalaData();
    private List<DangHui> detailAll_dangfei = new ArrayList<DangHui>();
    private List<DangHui> detailAll_huifei = new ArrayList<DangHui>();
    private String feeExpress = "";
    //

    public FeeCalBean() {
        //初始化年度列表
        iyearLi = CommonParam.yearList();
        cpsn_number = LoginParam.getPer().getCpsn_number();
    }
 

    public void iYearChange(ValueChangeEvent value) {
        iyear = (String) value.getNewValue();
    }

    public void feeCal_dangFei() {
        Map map1 = new HashMap();
        map1.put("orgCode", LoginParam.getOrg().getCode());
        String dangf = (String) IbatisTemplate.query("testconct.getFeeExpressDang", map1);//党费计算表达式
        System.out.println("党费计算表达式 : " + dangf);

        Map param = new HashMap();
        param.put("orgCode", LoginParam.getOrg().getCode());
        param.put("iyear", iyear);
        param.put("iMonth", imonth);
        param.put("feeExpress", dangf);
        List<DangHui> dangli = DangHuiImpl.calDangfei(param);//党费数据
        System.out.println("dangli : " + dangli.size());
        DangHuiImpl.update_dangfei(dangli);//保存党费数据
        queryList_dangfei();
    }

    public void feeCal_huiFei() {
        Map par = new HashMap();
        par.put("orgCode", LoginParam.getOrg().getCode());
        par.put("iyear", iyear);
        par.put("iMonth", imonth);
//        DangHuiImpl.deleteByIyear(par);

        String str = (String) IbatisTemplate.query("testconct.getFeeExpress", par);//会费计算表达式
        String feeRate = (String) IbatisTemplate.query("testconct.getFeeRate", par);//会费计算比率

        System.out.println("会费计算表达式 : " + str);
        System.out.println("会费计算比率 : " + feeRate);

        Map map = new HashMap();
        map.put("orgCode", LoginParam.getOrg().getCode());
        map.put("iyear", iyear);
        map.put("iMonth", imonth);
        map.put("feeExpress", str);
        map.put("feeRate", feeRate);
        List<DangHui> list = DangHuiImpl.calHuifei(map);//会费数据
        //循环迭代批处理
        DangHuiImpl.update_huifei(list);//保存会费数据
        queryList_huifei();
    }

    public void queryList_dangfei() {
        Map param = new HashMap();
        param.put("orgCode", LoginParam.getOrg().getCode());
        param.put("iyear", iyear);
        param.put("iMonth", imonth);
        detailAll_dangfei = DangHuiImpl.getDetail(param);
    }

    public void queryList_huifei() {
        Map param = new HashMap();
        param.put("orgCode", LoginParam.getOrg().getCode());
        param.put("iyear", iyear);
        param.put("iMonth", imonth);
        detailAll_huifei = DangHuiImpl.getDetail(param);
    }

    public void exportExcel_dangfei() {
        HSSFWorkbook demoWorkBook = new HSSFWorkbook();
        HSSFSheet demoSheet = demoWorkBook.createSheet("党费计算");
        String[] excelColumns = initTabeleHeader_dangfei();
        List<DangHui> rows = detailAll_dangfei;

        HSSFHeader header = demoSheet.getHeader();
        header.setCenter("党费计算");
        HSSFRow headerRow = demoSheet.createRow(0);
        for (int i = 0; i < excelColumns.length; i++) {
            HSSFCell headerCell = headerRow.createCell((short) i);
            //headerCell.setEncoding(HSSFCell.ENCODING_UTF_16);
            headerCell.setCellValue(excelColumns[i]);
        }
        //填充数据
        int rowIndex = 1;
        for (DangHui row : rows) {
            createTableRow_dangfei(demoWorkBook, demoSheet, row, rowIndex);
            rowIndex++;
        }
        exportExcel(demoWorkBook, demoSheet);

    }

    public void exportExcel_huifei() {
        HSSFWorkbook demoWorkBook = new HSSFWorkbook();
        HSSFSheet demoSheet = demoWorkBook.createSheet("会费计算");
        String[] excelColumns = initTabeleHeader_huifei();
        List<DangHui> rows = detailAll_huifei;

        HSSFHeader header = demoSheet.getHeader();
        header.setCenter("会费计算");
        HSSFRow headerRow = demoSheet.createRow(0);
        for (int i = 0; i < excelColumns.length; i++) {
            HSSFCell headerCell = headerRow.createCell((short) i);
            //headerCell.setEncoding(HSSFCell.ENCODING_UTF_16);
            headerCell.setCellValue(excelColumns[i]);
        }
        //填充数据
        int rowIndex = 1;
        for (DangHui row : rows) {
            createTableRow_huifei(demoWorkBook, demoSheet, row, rowIndex);
            rowIndex++;
        }
        exportExcel(demoWorkBook, demoSheet);

    }

    //初始化表头信息
    public String[] initTabeleHeader_dangfei() {
        String[] excelColumns = {"部门", "姓名", "党费"};
        return excelColumns;
    }

    //初始化表头信息
    public String[] initTabeleHeader_huifei() {
        String[] excelColumns = {"部门", "姓名", "会费"};
        return excelColumns;
    }

    public static void createTableRow_dangfei(HSSFWorkbook demoWorkBook, HSSFSheet demoSheet, DangHui eit, int rowIndex) {
        //创建第rowIndex行
        CreationHelper createHelper = demoWorkBook.getCreationHelper();
        HSSFCellStyle dstyle = demoWorkBook.createCellStyle();
        dstyle.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-mm-dd"));//格式
        dstyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));

        HSSFCell cell = null;
        HSSFRow row = demoSheet.createRow(rowIndex);
        cell = row.createCell(0);
        cell.setCellValue(PayCodeConstants.convertString(eit.getDe().getName()));

        cell = row.createCell(1);
        cell.setCellValue(PayCodeConstants.convertString(eit.getCpsn_name()));

        cell = row.createCell(2);
        cell.setCellValue(PayCodeConstants.convertDouble(eit.getDangfei()));
        cell.setCellStyle(dstyle);
    }

    public static void createTableRow_huifei(HSSFWorkbook demoWorkBook, HSSFSheet demoSheet, DangHui eit, int rowIndex) {
        //创建第rowIndex行
        CreationHelper createHelper = demoWorkBook.getCreationHelper();
        HSSFCellStyle dstyle = demoWorkBook.createCellStyle();
        dstyle.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-mm-dd"));//格式
        dstyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));

        HSSFCell cell = null;
        HSSFRow row = demoSheet.createRow(rowIndex);
        cell = row.createCell(0);
        cell.setCellValue(PayCodeConstants.convertString(eit.getDe().getName()));
        cell = row.createCell(1);
        cell.setCellValue(PayCodeConstants.convertString(eit.getCpsn_name()));

        cell = row.createCell(2);
        cell.setCellValue(PayCodeConstants.convertDouble(eit.getHuifei()));
        cell.setCellStyle(dstyle);
    }

    public static void exportExcel(HSSFWorkbook demoWorkBook, HSSFSheet demoSheet) {
        try {
            String strfileName = "book.xls";
            FileOutputStream fos = null;
            //导出
            demoSheet.setGridsPrinted(true);//demoSheet
            HSSFFooter footer = demoSheet.getFooter();
            footer.setRight("Page " + HSSFFooter.page() + " of " + HSSFFooter.numPages());
            // 获得JSF上下文环境
            FacesContext context = FacesContext.getCurrentInstance();
            // 获得ServletContext对象
            ServletContext servletContext = (ServletContext) context.getExternalContext().getContext();
            // 取得文件的绝对路径
            String excelName = servletContext.getRealPath("/upload") + "/" + strfileName;//strfileName
            File exportFile = new File(excelName);
            fos = new FileOutputStream(exportFile);//fos
            demoWorkBook.write(fos);//demoWorkBook
            fos.close();

            FacesContext ctx = FacesContext.getCurrentInstance();
            ctx.responseComplete();
            HttpServletResponse httpServletResponse = (HttpServletResponse) ctx.getExternalContext().getResponse();
            ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();
            httpServletResponse.setHeader("Content-disposition", "attachment; filename=" + strfileName);//strfileName
            httpServletResponse.setContentLength((int) exportFile.length());//exportFile
            httpServletResponse.setContentType("application/x-download");
            //httpServletResponse.setContentType("application/vnd.ms-excel");
            byte[] b = new byte[1024];
            int i = 0;
            FileInputStream fis = new java.io.FileInputStream(exportFile);
            while ((i = fis.read(b)) > 0) {
                servletOutputStream.write(b, 0, i);
            }
            servletOutputStream.flush();
            ctx.responseComplete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getCpsn_number() {
        return cpsn_number;
    }

    public void setCpsn_number(String cpsn_number) {
        this.cpsn_number = cpsn_number;
    }

    public DangHui getDang() {
        return dang;
    }

    public void setDang(DangHui dang) {
        this.dang = dang;
    }

    public SalaData getData() {
        return data;
    }

    public void setData(SalaData data) {
        this.data = data;
    }

    public List<DangHui> getDetailAll_dangfei() {
        return detailAll_dangfei;
    }

    public void setDetailAll_dangfei(List<DangHui> detailAll_dangfei) {
        this.detailAll_dangfei = detailAll_dangfei;
    }

    public List<DangHui> getDetailAll_huifei() {
        return detailAll_huifei;
    }

    public void setDetailAll_huifei(List<DangHui> detailAll_huifei) {
        this.detailAll_huifei = detailAll_huifei;
    }

    public FeeCal getFee() {
        return fee;
    }

    public void setFee(FeeCal fee) {
        this.fee = fee;
    }

    public String getFeeExpress() {
        return feeExpress;
    }

    public void setFeeExpress(String feeExpress) {
        this.feeExpress = feeExpress;
    }

    public String getImonth() {
        return imonth;
    }

    public void setImonth(String imonth) {
        this.imonth = imonth;
    }

    public String getIyear() {
        return iyear;
    }

    public void setIyear(String iyear) {
        this.iyear = iyear;
    }

    public List<SelectItem> getIyearLi() {
        return iyearLi;
    }

    public void setIyearLi(List<SelectItem> iyearLi) {
        this.iyearLi = iyearLi;
    }
}
